import * as Router from 'koa-router'
import * as Joi from 'joi'
import * as dingdanService from '../services/dingdans.service'
import { badParams, stats } from '../stats'

const router = new Router({
  prefix: '/api/dingdan'
})

// 下单书籍
router.post('/add', async ctx => {
  const user = await ctx.state.userInfo
  if (!user) throw stats.ERR_NOT_LOGIN
  const schema = Joi.object({
    cartIds: Joi.array().items(Joi.string().required()).min(1).required(),
    allprice: Joi.number().required(),
    phone: Joi.number().required(),
    name: Joi.string().required(),
    address: Joi.string().required(),
    note: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const msg = await dingdanService.addDingdan(value, user._id)
  ctx.body = {
    stat: 'OK',
    msg
  }
})

// 订单列表
router.post('/list', async ctx => {
  const schema = Joi.object({
    userId: Joi.string().required(),
    skip: Joi.number().required(),
    limit: Joi.number().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const data = await dingdanService.Dingdanlist(value)
  ctx.body = {
    stat: 'OK',
    data
  }
})

// 确认/取消收货
router.post('/confirm', async ctx => {
  const schema = Joi.object({
    cartId: Joi.string().required(),
    flag: Joi.boolean().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const user = await ctx.state.userInfo
  if (user) {
    await dingdanService.confirm(value, user._id)
    ctx.body = {
      stat: 'OK'
    }
  }
})

// 订单详情
router.post('/detail', async ctx => {
  const schema = Joi.object({
    dingdanId: Joi.string().required(),
    cartId: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const token = ctx.cookies.get('token')
  if (token) {
    const user = await ctx.state.userInfo
    const data = await dingdanService.detail(value, user._id)
    ctx.body = {
      stat: 'OK',
      data
    }
  }
})

// 书籍列表
router.post('/list/book', async ctx => {
  const user = await ctx.state.userInfo
  if (!user) throw stats.ERR_NOT_LOGIN
  const schema = Joi.object({
    bookIds: Joi.array().items(Joi.string().required()).min(1).required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const rows = await dingdanService.list(value.bookIds)
  ctx.body = {
    stat: 'OK',
    rows
  }
})

// 通知送货列表
router.post('/list/send', async ctx => {
  const user = await ctx.state.userInfo
  if (!user) throw stats.ERR_NOT_LOGIN
  const schema = Joi.object({
    skip: Joi.number().required(),
    limit: Joi.number().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const data = await dingdanService.sendlist(value, user._id)
  ctx.body = {
    stat: 'OK',
    data
  }
})

// 查看了通知
router.post('/list/see', async ctx => {
  const user = await ctx.state.userInfo
  if (!user) throw stats.ERR_NOT_LOGIN
  const schema = Joi.object({
    advId: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const res = await dingdanService.see(value.advId)
  if (res) {
    ctx.body = {
      stat: 'OK'
    }
  }
})

router.post('/list/deal', async ctx => {
  const user = await ctx.state.userInfo
  if (!user) throw stats.ERR_NOT_LOGIN
  const schema = Joi.object({
    advId: Joi.string().required(),
    flag: Joi.boolean().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  await dingdanService.arrival(value.advId, value.flag)
  ctx.body = {
    stat: 'OK'
  }
})

export default router
